package edu.maimonides.ejercicios.ides1.sql.ddl.hsql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class JDBCUtils {
	public static int executeStatement(String query, Connection connection) throws SQLException {
		Statement statement = connection.createStatement();
		try {
			statement.execute(query);
			return statement.getUpdateCount();
		} finally {
			statement.close();
		}
	}
	public static <T> List<T> executeQuery(String query, Connection connection,ResultSetObjectFactory<T> factory) throws SQLException {
		Statement statement = connection.createStatement();
		try {
			ResultSet rs = statement.executeQuery(query);
			List<T> results =  new ArrayList<T>(rs.getFetchSize());
			while(rs.next()){
				results.add(factory.createObject(rs));
			}
			return results;
		} finally {
			statement.close();
		}
	}
}
